package cn.edu.cdtu.healthserver.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cdtu.healthserver.domain.TestType;
import cn.edu.cdtu.healthserver.service.TestTypeService;
import cn.edu.cdtu.healthserver.service.impl.TestTypeServiceImplementation;
import cn.edu.cdtu.healthserver.utils.ConnectUtil;

import com.google.gson.Gson;

/**
 * Servlet implementation class TestTypeServletForClient
 */
@WebServlet(description = "面向Android客户端", urlPatterns = { "/TestTypeServletForClient" })
public class TestTypeServletForClient extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private TestTypeService testTypeService;
	private Gson gson;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestTypeServletForClient() {
        super();
        // TODO Auto-generated constructor stub
        testTypeService = new TestTypeServiceImplementation();
        gson = new Gson();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String option = request.getParameter(
				ConnectUtil.HealthTestPargrams.Option.HEALTH_TEST_OPTION);
		if(ConnectUtil.HealthTestPargrams.Option
				.OptionValue.HEALTH_TEST_OPTION_GET_TEST_TYPE.equals(option)){
			this.getTestTypeList(request, response);
		}
	}
	/**
	 * 
	 * @Title: getTestTypeList 
	 * @Description: TODO
	 * @return void
	 * @throws 
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getTestTypeList(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setContentType(ConnectUtil.ContentTypeString.jSON_STRING);
		List<TestType> testTypes = testTypeService.getTestTypes(false);
		OutputStream outputStream = response.getOutputStream();
		outputStream.write(gson.toJson(testTypes).getBytes());
		outputStream.flush();
		outputStream.close();
	}

}
